package com.example.administrator.homeworkintent;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * Created by Administrator on 2017/10/26.
 */

public class KeepThemes {
    public static String[] THEMES;
    private static String DEFAULT_THEME;
    private Context context;
    private String theme;

    public KeepThemes(Context context) {
        super();
        THEMES = context.getResources().getStringArray(R.array.chostheme);
        DEFAULT_THEME = THEMES[0];
        this.loadtheme();
    }

    private void loadtheme() {
        Properties properties = new Properties();
        try {
            FileInputStream putstream = context.openFileInput("configure.cfg");
            properties.load(putstream);
            theme = properties.getProperty("theme").toString();
        } catch (Exception e) {
            e.printStackTrace();
            this.savetheme(DEFAULT_THEME);
        }
    }

    private boolean savetheme(String OutputTheme) {
        Properties properties = new Properties();
        properties.put("theme", OutputTheme);
        FileOutputStream outstream = null;
        try {
            outstream = context.openFileOutput("configure.cfg", Context.MODE_PRIVATE);
            properties.store(outstream, " ");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public String gettheme() {
        return this.theme;
    }

    public void setAndSaveTheme(String theme) {
        this.theme = theme;
        this.savetheme(theme);
    }
}

